Title: x86assembly - selected Final topics

The Irvine procedure CreateOutputFile receives EDX as a pointer to a null-terminated byte array representing a filename and returns EAX as the file handle or INVALID_HANDLE_VALUE if it couldn't be created. The handle value in EAX can then be used with other procedures to write to the created and opened file.
Extra: The exact proc:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
CreateOutputFile PROC
    ;
    ; Creates a new file and opens it in output mode.
    ; Receives: EDX points to the filename.
    ; Returns: If the file was created successfully, EAX
    ;   contains a valid file handle. Otherwise, EAX
    ;   equals INVALID_HANDLE_VALUE.
    ;------------------------------------------------------
        INVOKE CreateFile,
          edx, GENERIC_WRITE, DO_NOT_SHARE, NULL,
          CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
        ret
    CreateOutputFile ENDP
Topic: x86final files

The Irvine procedure OpenFile opens a text file for input (reading) . It receives EDX as a pointer to a null-terminated byte array representing a filename and returns EAX as the valid file handle, or INVALID_HANDLE_VALUE if it failed to open.
Extra: 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
;------------------------------------------------------
    OpenFile PROC
    ;
    ; Opens a new text file and opens for input.
    ; Receives: EDX points to the filename.
    ; Returns: If the file was opened successfully, EAX
    ; contains a valid file handle. Otherwise, EAX equals
    ; INVALID_HANDLE_VALUE.
    ;------------------------------------------------------
        INVOKE CreateFile,
          edx, GENERIC_READ, DO_NOT_SHARE, NULL,
          OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0
        ret
    OpenFile ENDP
Topic: x86final files

The Irvine procedure ReadFromFile reads an input file into a buffer. It receives EAX as the file handle, EDX as the buffer address, and ECX as the number of bytes to read.
If the carry flag is 0, It returns EAX as the number of bytes read. But if the carry flag is 1, EAX is, instead, a system error code returned by the GetLastError Win32 function.
Extra: 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
;--------------------------------------------------------
    ReadFromFile PROC
    ;
    ; Reads an input file into a buffer.
    ; Receives: EAX = file handle, EDX = buffer offset,
    ;    ECX = number of bytes to read
    ; Returns: If CF = 0, EAX = number of bytes read; if
    ;    CF = 1, EAX contains the system error code returned
    ;    by the GetLastError Win32 API function.
    ;--------------------------------------------------------
    .data
    ReadFromFile_1 DWORD ?         ; number of bytes read
    .code
         INVOKE ReadFile,
             eax,                  ; file handle
             edx,                  ; buffer pointer
             ecx,                  ; max bytes to read
             ADDR ReadFromFile_1,  ; number of bytes read
             0                     ; overlapped execution flag
         mov eax,ReadFromFile_1
         ret
    ReadFromFile ENDP
Topic: x86final files

The Irvine Procedure WriteToFile writes a buffer to an output file.
It receives EAX as the file handle.
It receives EDX as the buffer address.
It receives ECX as the number of bytes to write.
It returns EAX as the number of bytes written to the file.
Extra: 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
;--------------------------------------------------------
    WriteToFile PROC
    ;
    ; Writes a buffer to an output file.
    ; Receives: EAX = file handle, EDX = buffer offset,
    ;    ECX = number of bytes to write
    ; Returns: EAX = number of bytes written to the file.
    ; If the value returned in EAX is less than the
    ; argument passed in ECX, an error likely occurred.
    ;--------------------------------------------------------
    .data
    WriteToFile_1 DWORD ?          ; number of bytes written
    .code
         INVOKE WriteFile,         ; write buffer to file
             eax,                  ; file handle
             edx,                  ; buffer pointer
             ecx,                  ; number of bytes to write
             ADDR WriteToFile_1,   ; number of bytes written
             0                     ; overlapped execution flag
         mov eax,WriteToFile_1     ; return value
         ret
    WriteToFile ENDP
    ;-------------------
Topic: x86final files

The Irvine procedure CloseFile closes a file, using its handle as the identifier.
It receives EAX as the file handle.
It returns a non-zero EAX if the file succesfully closed.
Extra: 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
;--------------------------------------------------------
    CloseFile PROC
    ;
    ; Closes a file using its handle as an identifier.
    ; Receives: EAX = file handle
    ; Returns: EAX = nonzero if the file is successfully
    ;  closed.
    ;--------------------------------------------------------
        INVOKE CloseHandle, eax
        ret
    CloseFile ENDP
Topic: x86final files

A floating point decimal number is encoded with three components.
The sign bit indicates whether it is positive or negative.
The significand or mantissa represents the leading decimal. It has an implicit leading bit and fraction bits.
The exponent is the power to multiply the mantissa / significand by.
Extra: Link to University of Florida PDF on IEEE representations of Floating Point numbers.
url: http://www.hlam.ece.ufl.edu/EEL4712/Labs/Lab6/IEEEStandard754FP.pdf
Topic: x86final floating point

The IEEE (Institute of Electrician and Electronics Engineers) standards for binary floating point number representation provide for these types of reals.
REAL4 - 32 bits - "short" real
REAL8 - 64 bits - "long" real
REAL10 - 80 bits - "extended" real
Extra: Link to info about assembly variable types
url: https://www.phatcode.net/res/223/files/html/Chapter_5/CH05-1.html#HEADING1-168
Topic: x86final floating point

Overview:
The Intel 8086 processor was designed for integer arithmetic. The 8087 was a separate Intel floating point processor. Since the 80486, the FPU has been integrated into the CPU.
The FPU does not use general purpose registers like eax, edx, etc. It has its own registers called the register stack.
The FPU loads values from memory on the stack and performs operations from the stack, placing the result onto the stack, much like the RPN calculator worked.
The FPU stack contains 8 accessible 80 bit register named R0 through R7. A 3 bit field named TOP identifies the top of the stack.
The top of the FP stack is known as ST(0) or ST
The last register of the FP stack is known as ST(7).
The stack is circular.
Extra: link to a big chapter on floating point stuff
url: https://www.phatcode.net/res/223/files/html/Chapter_14/CH14-1.html#top
Topic: x86final floating point

Q: FP instructions can have zero, one, or two operands.
When two operands are used, one must be a [...] [...] register.
[...] operands are not valid for FP operations.
A: floating; point; Immediate
url: https://www.phatcode.net/res/223/files/html/Chapter_5/CH05-1.html#HEADING1-168
Topic: x86final floating point

Q: The instruction to initialize the FPU unit, setting fp registers, pointers, and flags to their default state is:
1 | [...]
A: FINIT
Extra: link to felix cloutier finit instruction
url: https://www.felixcloutier.com/x86/finit:fninit
Topic: x86final floating point

Q: To store the real number 10.0 in 64 bits of memory with the identifier ten:
1
2
.data
  ten [...] [...]
A: REAL8; 10.0
Extra: Link to info about x86 memory storage
url: https://www.phatcode.net/res/223/files/html/Chapter_5/CH05-1.html#HEADING1-168
Topic: x86final floating point

Q: The instruction to load the Real8 number from memory referenced by the identifier ten onto the Floating Point stack is:
1 | [...] ten
A: FLD
Extra: link: phatcode, FLD info
url: https://www.phatcode.net/res/223/files/html/Chapter_14/CH14-4.html#HEADING4-5
Topic: x86final floating point

Q: The instruction to load the 32 bit integer number from memory referenced by the identifier "int_number" onto the floating point stack is:
1 | [...] int_number
A: FILD
Extra: link to phatcode, fild info
url: https://www.phatcode.net/res/223/files/html/Chapter_14/CH14-4.html#HEADING4-37
Topic: x86final floating point

Q: The instruction to divide the top of the floating point stack by the integer number in memory referenced by the identifier "int_divisor" and store the result on top of the floating point stack is:
1 | [...] int_divisor
A: FIDIV
Extra: link to felixcloutier.com info about fidiv
url: https://www.felixcloutier.com/x86/fdiv:fdivp:fidiv
Topic: x86final floating point

Q: The instruction to copy the floating point number from the 3rd position in the floating point stack to the top of the floating point stack is:
1 | [...] [...]
A: FLD; ST(2)
Extra: The fld instruction loads a 32 bit, 64 bit, or 80 bit floating point value onto the stack. This instruction converts 32 and 64 bit operand to an 80 bit extended precision value before pushing the value onto the floating point stack.
link to phatcode fld info
url: https://www.phatcode.net/res/223/files/html/Chapter_14/CH14-4.html#HEADING4-5
Topic: x86final floating point

Q: The instruction to load the constant PI onto the floating point stack is:
1 | [...]
A: FLDPI
Extra: link to felix cloutier info about all constant fpu loads
url: https://www.felixcloutier.com/x86/fld1:fldl2t:fldl2e:fldpi:fldlg2:fldln2:fldz
Topic: x86final floating point

Q: The instruction to copy the value from the top of the FP stack, ST(0), to the real number memory location identified by "temp_fp_number", without popping it, is:
1 | [...] temp_fp_number
A: FST
Extra: phatcode fst info
url: https://www.phatcode.net/res/223/files/html/Chapter_14/CH14-4.html#HEADING4-14
Topic: x86final floating point

Q: The instruction to copy the value from the top of the FP stack, ST(0), to itself, and pops it, effectively popping the top of the floating point stack without transferring any data, is:
1 | [...] st(0)
A: FSTP
Extra: FSTP (Floating Point Store Pop) copies before popping.
link to phatcode fst, fstp info
url: https://www.phatcode.net/res/223/files/html/Chapter_14/CH14-4.html#HEADING4-14
Topic: x86final floating point

Q: The instruction for translating the top of the FP stack to an integer and storing it in a dword memory location identified by my_int, without popping it from the stack, is:
1 | [...] my_int
Rounding occurs according to the rounding mode specified by the control field of the FPU control word. By default, after FINIT,  it rounds to the [...] whole number.
A: FIST; nearest
Extra: FISTP would do the same but with popping. You could also store in a 16 or 64 bit mem location.
Link to phatcode info about load and store instructions for fpu.
url: https://www.phatcode.net/res/223/files/html/Chapter_14/CH14-4.html#HEADING4-43
Topic: x86final floating point

Q: The instruction to pop the top of the FP stack and store the value as an integer in a memory location identified by my_integer_mem_loc, truncating off the decimal portion regardless of the FP rounding settings, is:
1 | [...] my_integer_mem_loc
A: FISTTP
Extra: Actually a pretty key instruction for printing floating point numbers, because you can store the front part of the number in one location, multiply by 100, then store again in a second location, then just print both those locations with a dot in between them to print a representaiton of the floating point to hundreths precision.
link to felix cloutier info about fisttp instruction
url: https://www.felixcloutier.com/x86/fisttp
Topic: x86final floating point

Q: The instruction to add ST(0) to ST(1) and push the result on to the floating point register stack is:
1 | [...] ST(0), ST(1)
A: FADD
Extra: FIADD can take an integer source operand.
FADDP will also pop ST(0)
Link to felix cloutier info about fadd
url: https://www.felixcloutier.com/x86/fadd:faddp:fiadd
Topic: x86final floating point

Q: To convert an integer memory location given by the identifier int_mem_loc to a floating point number, multiply it with ST(0), and store the result on top of the FP register stack, use the instruction:
1 | [...] int_mem_loc
A: FIMUL
Extra: link to felix cloutier, fimul, fmul, fmulp info
url: https://www.felixcloutier.com/x86/fmul:fmulp:fimul
Topic: x86final floating point